package indi.madoka.weeb.module.base.plugin;

import indi.madoka.weeb.annotations.Keyword;
import indi.madoka.weeb.annotations.Plugin;
import indi.madoka.weeb.bean.send.Sender;
import indi.madoka.weeb.bean.update.message.UpdateMessage;
import indi.madoka.weeb.enums.MatchType;
import indi.madoka.weeb.module.base.service.SetuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Arcueid
 */
@Plugin("Picture Searcher")
@Slf4j
public class SetuPlugin {
    private SetuService setuService;
    @Autowired
    public void setSetuService(SetuService setuService){
        this.setuService = setuService;
    }

    @Keyword(value = "#涩图", matchType = MatchType.EQUALS, description = "获取随机涩图")
    public void randomPicture(UpdateMessage updateMessage) {
        String randomPictureUrl = setuService.getRandomPicture();
        if(StringUtils.isEmpty(randomPictureUrl)){
            new Sender.Builder(updateMessage).addText("找不到图片")
                    .build()
                    .sendAll();
        }else{
            new Sender.Builder(updateMessage).addImage(randomPictureUrl)
                    .build()
                    .sendAll();
        }
    }

    @Keyword(value = "#涩图 ", param = "[关键字]",matchType = MatchType.STARTS_WITH, description = "搜索相关涩图")
    public void randomPictureByTag(UpdateMessage updateMessage){
        String tag = updateMessage.getRawMessage().substring(4);
        String taggedPictureUrl = setuService.getTaggedPicture(tag);
        if(StringUtils.isEmpty(taggedPictureUrl)){
            new Sender.Builder(updateMessage).addText("找不到图片")
                    .build()
                    .sendAll();
        }else{
            new Sender.Builder(updateMessage).addImage(taggedPictureUrl)
                    .build()
                    .sendAll();
        }
    }

    @Keyword(value = "#uid ", param = "[uid]", matchType = MatchType.STARTS_WITH, description = "搜索画师uid涩图")
    public void getPictureByPid(UpdateMessage updateMessage){
        String uid = updateMessage.getRawMessage().substring(5);
        String uidPictureUrl = setuService.getUidPicture(uid);
        log.info(uidPictureUrl);
        if(StringUtils.isEmpty(uidPictureUrl)){
            new Sender.Builder(updateMessage).addText("找不到图片")
                    .build()
                    .sendAll();
        }else{
            new Sender.Builder(updateMessage).addImage(uidPictureUrl)
                    .build()
                    .sendAll();
        }
    }
}
